package leetcode.code1591;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public boolean isPrintable(int[][] targetGrid) {
		int m = targetGrid.length, n = targetGrid[0].length;
		// 1.获得每个矩形的范围
		Map<Integer, int[]> ranges = new HashMap<>();// int[]{rowLeft,rowRight,colLeft,colRight}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				int cur = targetGrid[i][j];
				int[] range = ranges.getOrDefault(cur, new int[] { 61, 0, 61, 0 });
				ranges.put(cur, new int[] { Math.min(range[0], i), Math.max(range[1], i), Math.min(range[2], j),
						Math.max(range[3], j) });
			}
		}
		// 2.获得每个矩形的叠加顺序
		Map<Integer, Set<Integer>> edges = new HashMap<>();// Map<上面的矩形,Set<下面的矩形>>
		Map<Integer, Set<Integer>> inEdges = new HashMap<>();// Map<下面的矩形,Set<上面的矩形>>
		for (Map.Entry<Integer, int[]> entry : ranges.entrySet()) {
			int to = entry.getKey();
			int[] range = entry.getValue();
			int from;
			for (int i = range[0]; i <= range[1]; i++) {
				for (int j = range[2]; j <= range[3]; j++) {
					if ((from = targetGrid[i][j]) != to) {// 矩形里出现了其它数字，这个数字一定叠压在当前矩形上边
						Set<Integer> edge = edges.getOrDefault(from, new HashSet<>());
						edge.add(to);
						edges.put(from, edge);
						Set<Integer> inEdge = inEdges.getOrDefault(to, new HashSet<>());
						inEdge.add(from);
						inEdges.put(to, inEdge);
					}
				}
			}
		}
		// 3.入度，为了拓扑排序
		Integer[] ins = new Integer[61];
		for (Map.Entry<Integer, int[]> entry : ranges.entrySet()) {
			int to = entry.getKey();
			ins[to] = inEdges.containsKey(to) ? inEdges.get(to).size() : 0;
		}
		// 4.拓扑排序，决定处理的矩形顺序
		Queue<Integer> queue = new LinkedList<>();
		for (int rectangle : ranges.keySet()) {
			if (ins[rectangle] != null && ins[rectangle] == 0) {
				queue.add(rectangle);
			}
		}
		while (!queue.isEmpty()) {
			int cur = queue.poll();
			if (!edges.containsKey(cur)) {//
				break;
			}
			int[] range = ranges.get(cur);
			// 4.1 将上边的矩形涂成白色
			for (int i = range[0]; i <= range[1]; i++) {
				for (int j = range[2]; j <= range[3]; j++) {
					targetGrid[i][j] = 0;
				}
			}
			Set<Integer> edge = edges.get(cur);
			for (int to : edge) {
				ins[to] -= 1;
				if (ins[to] == 0) {
					// 4.2 作为露在顶上的矩形，它上面的颜色要么是本色to，要么是白色0
					range = ranges.get(to);
					for (int i = range[0]; i <= range[1]; i++) {
						for (int j = range[2]; j <= range[3]; j++) {
							if (!(targetGrid[i][j] == to || targetGrid[i][j] == 0)) {
								return false;
							}
						}
					}
					queue.add(to);
				}
			}
		}
		// 有处理不完的矩形
		for (Integer in : ins) {
			if (in != null) {
				if (in != 0) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(true, this.isPrintable(HelpDebug.str2array1("[[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]")));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(true, this.isPrintable(HelpDebug.str2array1("[[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]")));

	}

	@Override
	public void debug3() {
		HelpDebug.compare(false, this.isPrintable(HelpDebug.str2array1("[[1,2,1],[2,1,2],[1,2,1]]")));

	}

	@Override
	public void debug4() {
		HelpDebug.compare(false, this.isPrintable(HelpDebug.str2array1("[[1,1,1],[3,1,3]]")));

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
